package com.itheima.crm.service.impl;

import com.itheima.crm.mapper.CustomerMapper;
import com.itheima.crm.pojo.Customer;
import com.itheima.crm.pojo.QueryVo;
import com.itheima.crm.service.CustomerService;
import com.itheima.crm.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    public Page<Customer> search(QueryVo queryVo) {
        //计算SQL语句中,从哪一条数据开始查,每页显示10条数据
        //第1页从0开始,第2页从10开始,第3页从20开始...
        //从哪一条数据开始查:(page-1)*size
        //设置start,从哪一条数据开始
        queryVo.setStart((queryVo.getPage() - 1) * queryVo.getSize());

        //根据条件分页查询客户结果集
        List<Customer> list = customerMapper.queryCustomerByQueryVo(queryVo);

        //根据条件查询数据总记录数
        int total = customerMapper.queryCountByQueryVo(queryVo);

        //把查询到的数据封装到page对象中
        Page<Customer> page = new Page<Customer>(total, queryVo.getPage()
                , queryVo.getSize(), list);


        //返回page
        return page;
    }

    public Customer queryById(Long id) {
        Customer customer = customerMapper.queryById(id);
        return customer;
    }

    public void updateById(Customer customer) {
        customerMapper.updateById(customer);
    }

    public void deleteById(Long id) {
        customerMapper.deleteById(id);
    }
}
